package com.catbad.controller;

import com.catbad.annotations.Auth;
import com.catbad.domain.User;
import com.catbad.services.PlayServices;
import com.catbad.services.UserServices;
import com.catbad.vo.PlayVO;
import com.catbad.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class PersonnelPlayController {
    @Autowired
    UserServices userServices;
    @Autowired
    PlayServices playServices;
    /**
     * 进入排片管理页面
     * @return
     */
    @Auth("com.catbad.personnel.play")
    @RequestMapping("personnelplay")
    public String personnelPlay(Model model){
        return "personnel/playTime";
    }

    /**
     * 按日期排片的页面显示信息
     * @param valTime
     * @param model
     * @param request
     * @return
     */
    @Auth("com.catbad.personnel.play")
    @RequestMapping("toPlay")
    public String toPlay(String valTime, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();  //通过user的账号关联到影院，获取影院id
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        PlayVO playVO =  playServices.getPlayForPage(cid,valTime);  //根据时间得到排片信息
        model.addAttribute("playVO",playVO);
        model.addAttribute("valTime",valTime);
        return "personnel/play";
    }

    /**
     * 删除场次
     * @param valTime
     * @param filmId
     * @return
     */
    @Auth("com.catbad.personnel.play")
    @RequestMapping("deleteDatePlay")
    @ResponseBody
    public Integer deleteDatePlay(String valTime,Long filmId,HttpServletRequest request){
        HttpSession session = request.getSession();  //通过user的账号关联到影院，获取影院id
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        return playServices.deletePlayForTime(valTime,cid,filmId);//删除掉该天，该影院，该电影所有排片;
    }

    /**
     * 保存场次
     * @return
     */
    @Auth("com.catbad.personnel.play")
    @RequestMapping("savePlay")
    @ResponseBody
    public Integer savePlay(Long filmId,String[] msg,String time,HttpServletRequest request){
        if(filmId==null || filmId.equals("")){
            return 1;
        }
        HttpSession session = request.getSession();  //通过user的账号关联到影院，获取影院id
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        try {
            return playServices.savePlay(filmId,msg,time,cid);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Auth("com.catbad.personnel.play")
    @RequestMapping("editPlay")
    @ResponseBody
    public Integer editPlay(Long filmId,String[] msg,String time,HttpServletRequest request){
        HttpSession session = request.getSession();  //通过user的账号关联到影院，获取影院id
        User user = (User)session.getAttribute("user");
        String account = user.getUaccount();
        Long cid = userServices.getCinemaId(account);
        playServices.deletePlayForTime(time,cid,filmId);//删除掉该天，该影院，该电影所有排片
        try {
            return playServices.savePlay(filmId,msg,time,cid);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
